var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0}), (i.GridListViewEvent = void 0);
var s,
    c,
    l = e("CCPoolManager"),
    d = cc._decorator,
    u = d.ccclass,
    p = d.property,
    h = d.requireComponent;
((c = s = i.GridListViewEvent || (i.GridListViewEvent = {}))[(c.DELEGATE = 0)] = "DELEGATE"),
    (c[(c.INITFINITE = 1)] = "INITFINITE"),
    (c[(c.DELEGATE_UPDATE = 2)] = "DELEGATE_UPDATE");
var f = (function (e) {
    function t() {
        var t = (null !== e && e.apply(this, arguments)) || this;
        return (
            (t.prefab = null),
            (t.nodeItem = null),
            (t.poolKey = ""),
            (t.totalCount = 0),
            (t.spaceX = 5),
            (t.spaceY = 5),
            (t.buffList = []),
            (t.items = []),
            (t.lastContentPos = 0),
            (t.contentSize = 0),
            (t.position_back = 0),
            (t.position_front = 0),
            (t.offset = 0),
            (t.bufferSize = 0),
            (t.itemSize = 0),
            (t.spaceSize = 0),
            (t.bufferZone = 0),
            (t.cellNum = 1),
            (t.needCount = 0),
            (t.onDelegate = null),
            (t.onInitFinish = null),
            (t.onDelegateUpdate = null),
            t
        );
    }
    return (
        a(t, e),
        (t.prototype.start = function () {
            (this.scrollView = this.getComponent(cc.ScrollView)),
                this.scrollView &&
                    (this.prefab || this.nodeItem) &&
                    ((this.content = this.scrollView.content),
                    this.content.removeAllChildren(),
                    this.scrollView.node.on("scrolling", this.updateItem, this),
                    this.init());
        }),
        (t.prototype.registerEvent = function (e, t, o) {
            e === s.DELEGATE
                ? (this.onDelegate = t.bind(o))
                : e === s.INITFINITE
                ? (this.onInitFinish = t.bind(o))
                : e === s.DELEGATE_UPDATE && (this.onDelegateUpdate = t.bind(o));
        }),
        (t.prototype.onDestroy = function () {
            this.clear();
        }),
        (t.prototype.reset = function (e) {
            this.scrollView.stopAutoScroll(),
                this.clear(),
                (this.totalCount = e),
                this.init(),
                this.scrollView.vertical ? this.scrollView.scrollToTop() : this.scrollView.scrollToLeft();
        }),
        (t.prototype.updateItems = function () {
            if (this.onDelegateUpdate)
                for (var e = 0; e < this.items.length; e++) {
                    var t = this.items[e];
                    this.onDelegateUpdate(t);
                }
        }),
        (t.prototype.clear = function () {
            var e = this;
            this.items &&
                this.items.length > 0 &&
                this.items.forEach(function (t) {
                    e.setItemActive(t, !1), e.putItem(t);
                }),
                (this.items.length = 0);
        }),
        (t.prototype.putItem = function (e) {
            this.poolKey ? l.default.instance().put(e) : this.buffList.push(e);
        }),
        (t.prototype.getItem = function () {
            return this.poolKey
                ? l.default.instance().get(this.poolKey)
                : this.prefab
                ? this.buffList.length > 0
                    ? this.buffList.shift()
                    : cc.instantiate(this.prefab)
                : this.nodeItem
                ? this.buffList.length > 0
                    ? this.buffList.shift()
                    : cc.instantiate(this.nodeItem)
                : (cc.error(" 预制体没有赋值 "), null);
        }),
        (t.prototype.init = function () {
            this.initParam(), this.addItems();
        }),
        (t.prototype.initParam = function () {
            (this.position_back = -1), (this.position_front = 0);
            var e = this.getItem();
            this.itemSize = 0;
            for (var t = 0, o = null, n = 0; n < this.scrollView.node.childrenCount; n++) {
                var i = this.scrollView.node.children[n];
                if (i.getComponent(cc.Mask)) {
                    o = i;
                    break;
                }
            }
            if (o) {
                this.scrollView.vertical
                    ? ((this.itemSize = e.height + this.spaceY),
                      (this.spaceSize = e.width + this.spaceX),
                      (this.bufferSize = o.height),
                      (this.content.width = o.width),
                      (this.content.anchorX = 0.5),
                      (this.content.anchorY = 1),
                      (this.content.x = 0),
                      (this.content.y = o.height / 2),
                      (t = this.content.width))
                    : ((this.itemSize = e.width + this.spaceX),
                      (this.spaceSize = e.height + this.spaceY),
                      (this.bufferSize = o.width),
                      (this.content.height = o.height),
                      (this.content.anchorX = 0),
                      (this.content.anchorY = 0.5),
                      (this.content.x = o.width / 2),
                      (this.content.y = 0),
                      (t = this.content.height)),
                    (this.cellNum = Math.floor(t / this.spaceSize)),
                    this.cellNum <= 0 && (this.cellNum = 1),
                    this.putItem(e),
                    (this.bufferSize += this.itemSize);
                var a = Math.floor(this.totalCount / this.cellNum);
                (this.needCount = this.totalCount % this.cellNum != 0 ? a + 1 : a),
                    (this.contentSize = this.needCount * this.itemSize),
                    this.scrollView.vertical
                        ? (this.content.height = this.contentSize)
                        : (this.content.width = this.contentSize),
                    this.scrollView.scrollTo(cc.v2(0, 1), 0);
            } else cc.error("不能没有Mask组件");
        }),
        (t.prototype.addItems = function () {
            var e,
                t = this.bufferSize,
                o = Math.floor(t / this.itemSize),
                n = t % this.itemSize == 0 ? o : o + 1;
            n > this.needCount && (n = this.needCount),
                (e = this.scrollView.vertical
                    ? 0 - ((this.cellNum - 1) * this.spaceSize) / 2
                    : ((this.cellNum - 1) * this.spaceSize) / 2);
            for (var i = 0; i < n; i++) {
                this.position_back++;
                for (var a = 0; a < this.cellNum; a++) {
                    var r = this.getItem();
                    this.addChild(r), this.items.push(r);
                    var s = this.position_back * this.cellNum + a;
                    if (s >= this.totalCount) this.setItemActive(r, !1);
                    else {
                        if (this.scrollView.vertical) {
                            var c = -(r.height + this.spaceY) * (0.5 + i),
                                l = e + (r.width + this.spaceX) * a;
                            this.setItemPosition(r, l, c);
                        } else
                            (l = (r.width + this.spaceX) * (0.5 + i)),
                                (c = e - (r.height + this.spaceY) * a),
                                this.setItemPosition(r, l, c);
                        this.onDelegate(this, s, r);
                    }
                }
            }
            var d = this.items.length / this.cellNum;
            (d = this.items.length % this.cellNum == 0 ? d : d + 1),
                (this.offset = this.itemSize * d),
                (this.bufferZone = this.bufferSize / 2),
                this.onInitFinish && this.onInitFinish(this);
        }),
        (t.prototype.setItemPosition = function (e, t, o) {
            e.setPosition(t, o);
        }),
        (t.prototype.setItemPositionX = function (e, t) {
            e.x = t;
        }),
        (t.prototype.setItemPositionY = function (e, t) {
            e.y = t;
        }),
        (t.prototype.setItemActive = function (e, t) {
            e.active = t;
        }),
        (t.prototype.addChild = function (e) {
            e.parent ? this.setItemActive(e, !0) : this.content.addChild(e);
        }),
        (t.prototype.getPositionInView = function (e) {
            var t = e.parent.convertToWorldSpaceAR(e.position);
            return this.scrollView.node.convertToNodeSpaceAR(t);
        }),
        (t.prototype.updateItem = function () {
            var e,
                t = this.items;
            e = this.scrollView.vertical
                ? this.scrollView.content.y < this.lastContentPos
                : this.scrollView.content.x > this.lastContentPos;
            for (var o = 0; o < this.items.length; o += this.cellNum)
                if (t[o]) {
                    var n = this.getPositionInView(t[o]);
                    if (this.scrollView.vertical) {
                        if (e) {
                            if (n.y < -this.bufferZone && t[o].y + this.offset < 0) {
                                this.position_front--, this.position_back--;
                                for (var i = 0; i < this.cellNum; i++)
                                    if ((r = t[o + i])) {
                                        this.setItemPositionY(r, r.y + this.offset);
                                        var a = this.position_front * this.cellNum + i;
                                        r.active || this.setItemActive(r, !0), this.onDelegate(this, a, r);
                                    }
                            }
                        } else if (n.y > this.bufferZone && t[o].y - this.offset > -this.contentSize)
                            for (this.position_front++, this.position_back++, i = 0; i < this.cellNum; i++)
                                (r = t[o + i]) &&
                                    (this.setItemPositionY(r, r.y - this.offset),
                                    (a = this.position_back * this.cellNum + i) >= this.totalCount
                                        ? this.setItemActive(r, !1)
                                        : (r.active || this.setItemActive(r, !0), this.onDelegate(this, a, r)));
                    } else if (e) {
                        if (n.x > this.bufferZone && t[o].x - this.offset > 0)
                            for (this.position_front--, this.position_back--, i = 0; i < this.cellNum; i++)
                                (r = t[o + i]) &&
                                    (r.active || this.setItemActive(r, !0),
                                    this.setItemPositionX(r, r.x - this.offset),
                                    this.onDelegate(this, this.position_front * this.cellNum + i, r));
                    } else if (n.x < -this.bufferZone && t[o].x + this.offset < this.contentSize)
                        for (this.position_front++, this.position_back++, i = 0; i < this.cellNum; i++) {
                            var r;
                            (r = t[o + i]) &&
                                (this.setItemPositionX(r, r.x + this.offset),
                                (a = this.position_back * this.cellNum + i) >= this.totalCount
                                    ? this.setItemActive(r, !1)
                                    : (r.active || this.setItemActive(r, !0), this.onDelegate(this, a, r)));
                        }
                }
            this.scrollView.vertical
                ? (this.lastContentPos = this.scrollView.content.y)
                : (this.lastContentPos = this.scrollView.content.x);
        }),
        (t.prototype.scrollToOffset = function (e, t) {
            if ((void 0 === t && (t = 1), this.scrollView)) {
                var o = cc.v2();
                this.scrollView.vertical ? (o.y = e * this.itemSize) : (o.x = e * this.itemSize),
                    this.scrollView.scrollToOffset(o, t);
            }
        }),
        (t.prototype.scrollToBottom = function (e) {
            void 0 === e && (e = 1), this.scrollView && this.scrollView.scrollToBottom(e);
        }),
        (t.prototype.scrollToTop = function (e) {
            void 0 === e && (e = 1), this.scrollView && this.scrollView.scrollToTop(e);
        }),
        (t.prototype.remove = function () {}),
        r([p(cc.Prefab)], t.prototype, "prefab", void 0),
        r([p(cc.Node)], t.prototype, "nodeItem", void 0),
        r([p], t.prototype, "poolKey", void 0),
        r([p], t.prototype, "totalCount", void 0),
        r([p], t.prototype, "spaceX", void 0),
        r([p], t.prototype, "spaceY", void 0),
        r([u, h(cc.ScrollView)], t)
    );
})(cc.Component);
i.default = f;
